package git.soulbgm.config;

import git.soulbgm.common.okhttp.OkHttpUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * OkHttp配置
 *
 * @author SoulBGM
 * @date 2022/03/08 10:16
 */
@Configuration
@ConfigurationProperties(prefix = "ok.http", ignoreUnknownFields = false)
public class OkHttpConfig {

    /**
     * 最大重试次数 默认2次
     */
    private Integer maxRetry = 2;
    /**
     * 重试延迟 默认500毫秒
     */
    private Duration delay = Duration.ofMillis(500);

    public Integer getMaxRetry() {
        return maxRetry;
    }

    public void setMaxRetry(Integer maxRetry) {
        this.maxRetry = maxRetry;
        init();
    }

    public Duration getDelay() {
        return delay;
    }

    public void setDelay(Duration delay) {
        this.delay = delay;
        init();
    }

    private void init() {
        if (maxRetry != null && delay != null) {
            OkHttpUtil.initOkHttpClient(maxRetry, delay.toMillis());
        }
    }
}
